<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>文字贴图 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,bootstrap-checkbox,bootstrap-slider,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <input type="button" class="btn btn-primary" value="竖立墙" onclick="drawWall()" />
      <input type="button" class="btn btn-primary" value="贴地矩形" onclick="drawRectangle()" />
      <input type="button" class="btn btn-primary" value="贴地矩形2" title="根据中心点和长宽来计算矩形" onclick="drawPoint()" />
      <input type="button" class="btn btn-primary" value="清除" onclick="removeAll()" />

      <table class="mars-table">
        <tbody>
          <tr>
            <td>方向:</td>
            <td>
              <input id="txtAngle"/>
            </td>
          </tr>
          <tr>
            <td>文字:</td>
            <td>
              <input id="txtName" type="text" class="form-control" value="Mars3D 火星科技 2021" />
            </td>
          </tr>
          <tr>
            <td></td>
            <td>
              <button id="btnOK" class="btn btn-primary">确定</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      var graphicLayer;
      var textMaterialProperty;

      var rotation = 0;
      function getRotationValue() {
        return rotation;
      }

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { lat: 29.792325, lng: 121.480055, alt: 146, heading: 198, pitch: -54 },
          },
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        //加个模型
        var tiles3dLayer = new mars3d.layer.TilesetLayer({
          name: "水利闸门",
          url: "//data.mars3d.cn/3dtiles/max-fsdzm/tileset.json",
          position: { alt: 15.2 },
          maximumScreenSpaceError: 1,
          maximumMemoryUsage: 1024,
        });
        map.addLayer(tiles3dLayer);

        //创建矢量数据图层
        graphicLayer = new mars3d.layer.GraphicLayer({
          hasEdit: true,
        });
        map.addLayer(graphicLayer);

        //材质对象
        textMaterialProperty = mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.Text, {
          text: "火星科技Mars3D平台",
          fillColor: new Cesium.Color(1.0, 1.0, 0.0, 1.0),
          stroke: true,
          strokeWidth: 2,
          strokeColor: new Cesium.Color(1.0, 1.0, 1.0, 0.8),
        });
        $("#btnOK").click(function () {
          textMaterialProperty.text = $("#txtName").val();
        });

        $("#txtAngle")
          .slider({
            min: 0,
            max: 360,
            step: 1,
            value: 0,
          })
          .on("slide", (e) => {
            rotation = Cesium.Math.toRadians(e.value);
          });

        //加一些演示数据
        addGraphic_01(graphicLayer);
        addGraphic_02(graphicLayer);
        addGraphic_03(graphicLayer);
        addGraphic_04(graphicLayer);
      }

      //wall文字 entity方式
      function addGraphic_01(graphicLayer) {
        var wallEntity = new mars3d.graphic.WallEntity({
          positions: [
            [121.479914, 29.791249, 32],
            [121.479694, 29.791303, 32],
          ],
          style: {
            diffHeight: 5,

            materialType: mars3d.MaterialType.Text,
            text: "水利闸门",
            font_family: "楷体",
            fillColor: "#00ffff",
          },
        });

        graphicLayer.addGraphic(wallEntity);
      }

      //  wall文字  primitive方式添加
      function addGraphic_02(graphicLayer) {
        var primitive = new mars3d.graphic.WallEntity({
          positions: [
            [121.479343, 29.791419, 35],
            [121.479197, 29.791474, 35],
          ],
          style: {
            diffHeight: 5,
            materialType: mars3d.MaterialType.Text,
            text: "   火星科技",
            font_size: 70,
            fillColor: "#3388cc",
            outlineWidth: 4,

            onCustomCanvas: onCustomCanvas, //对Canvas做自定义处理
          },
        });
        graphicLayer.addGraphic(primitive);
      }

      //对Canvas做自定义处理,需要返回Promise
      function onCustomCanvas(canvas, material) {
        let cWidth = canvas.width;
        let cHeight = canvas.height;
        let context = canvas.getContext("2d");

        return Cesium.Resource.createIfNeeded("./img/country/zg.png")
          .fetchImage()
          .then((image) => {
            context.drawImage(image, 5, 5, 20, 20);

            return canvas;
          });
      }

      //rectangle贴地矩形  3dtiles路面文字
      function addGraphic_03(graphicLayer) {
        var rectangleEntity = new mars3d.graphic.RectangleEntity({
          name: "路面文字",
          positions: [
            [121.479989, 29.791162],
            [121.480114, 29.791201],
          ],
          style: {
            classificationType: Cesium.ClassificationType.BOTH,
            clampToGround: true,

            materialType: mars3d.MaterialType.Text,
            text: "火星路",
            font_size: 50,
            font_family: "楷体",
            fillColor: "#00ff00",
            stroke: true,
            strokeWidth: 2,
            strokeColor: "#ffffff",
            rotationDegree: 163,

            //高亮时的样式（默认为鼠标移入，也可以指定type:'click'单击高亮），构造后也可以openHighlight、closeHighlight方法来手动调用
            highlight: {
              type: mars3d.EventType.click,
              stroke: true,
              strokeColor: "#ff0000",
              strokeWidth: 4,
            },
          },
        });
        graphicLayer.addGraphic(rectangleEntity);
      }

      function addGraphic_04(graphicLayer) {
        var rectangleEntity = new mars3d.graphic.RectangleEntity({
          positions: [
            [121.479593, 29.791632, 13],
            [121.480136, 29.79169, 13],
          ],
          style: {
            material: textMaterialProperty,
            rotationDegree: 163,
          },
        });
        graphicLayer.addGraphic(rectangleEntity);
      }

      function drawWall() {
        graphicLayer.startDraw({
          type: "wall",
          maxPointNum: 2,
          style: {
            font_size: 50,
            color: "#ffff00",
            opacity: 0.2,
            outline: false,
            diffHeight: 5,
            material: textMaterialProperty,
          },
          success: function (graphic) {
            console.log(JSON.stringify(graphic.coordinates)); //打印下边界
          },
        });
      }

      function drawRectangle() {
        graphicLayer.startDraw({
          type: "rectangle",
          style: {
            material: textMaterialProperty,
            rotation: new Cesium.CallbackProperty(getRotationValue, false),
            stRotation: new Cesium.CallbackProperty(getRotationValue, false),
            clampToGround: true,
          },
          success: function (graphic) {
            console.log(JSON.stringify(graphic.coordinates)); //打印下边界
          },
        });
      }

      //根据中心点来计算矩形
      function drawPoint() {
        graphicLayer.startDraw({
          type: "point",
          style: {
            color: "#ffff00",
            opacity: 0.2,
            clampToGround: true,
          },
          success: function (graphic) {
            var position = graphic.positionShow;
            graphic.remove();

            var positions = mars3d.PolyUtil.getRectPositionsByCenter({
              center: position,
              width: 60,
              height: 10,
            });
            console.log(positions);

            var rectangleEntity = new mars3d.graphic.RectangleEntity({
              positions: positions,
              style: {
                material: textMaterialProperty,
                rotation: new Cesium.CallbackProperty(getRotationValue, false),
                stRotation: new Cesium.CallbackProperty(getRotationValue, false),
                clampToGround: true,
              },
            });
            graphicLayer.addGraphic(rectangleEntity);
          },
        });
      }

      function removeAll() {
        graphicLayer.clear();
      }
    </script>
  </body>
</html>
